package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.ChangeClass;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.mapper.ChangeClassMapper;
import com.yuyou.fn.educational.service.IChangeClassService;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IStudentService;
import com.yuyou.fn.educational.vo.ChangeClassDetailVo;
import com.yuyou.fn.educational.vo.ChangeClassQueryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 * ChangeClass 表数据服务层接口实现类
 *
 */
@Service
public class ChangeClassServiceImpl extends SuperServiceImpl<ChangeClassMapper, ChangeClass,Long> implements IChangeClassService {

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IStudentService studentService;

    @Override
    public Page<ChangeClassDetailVo> findChangeClassDetailPage(ChangeClassQueryVo changeClassQueryVo, int pageNo, int pageSize) {

        Example<ChangeClass> example=ChangeClass.createExample();
        Example.Criteria criteria=example.createCriteria();
        CriteriaUtils.fillCondition(criteria,changeClassQueryVo);
        example.orderBy("createTime").desc();
        Page<ChangeClass> changeClassPage=this.findPageByExample(example,pageNo,pageSize);
        Page<ChangeClassDetailVo> changeClassDetailVoPage=Utils.newPage(changeClassPage);
        List<ChangeClass> changeClassList=changeClassPage.getRecords();
        if (Utils.checkListNotEmpty(changeClassList)){
            List<ChangeClassDetailVo> changeClassDetailVoList=new ArrayList<>(changeClassList.size());
            List<Long> allClassIdList=new ArrayList<>(changeClassList.size()*2);
            allClassIdList.addAll(BeanUtils.toList(changeClassList,"oldClassId"));
            allClassIdList.addAll(BeanUtils.toList(changeClassList,"classId"));
            allClassIdList=Utils.removeDuplicate(allClassIdList);

            List<Long> studentIds=BeanUtils.toList(changeClassList,"studentId");
            List<Student> studentList=studentService.findByIds(studentIds);
            Map<Long, Student> studentMap = BeanUtils.toMap(studentList, "studentId");
            List<CourseClass> courseClassList = courseClassService.findByIds(allClassIdList,CourseClass.BASE_FIELDS);
            Map<Long,CourseClass> courseClassMap=BeanUtils.toMap(courseClassList,"classId");

            for (ChangeClass changeClass:changeClassList){
                CourseClass oldCourseClass=courseClassMap.get(changeClass.getOldClassId())==null?new CourseClass():courseClassMap.get(changeClass.getOldClassId());
                CourseClass courseClass=courseClassMap.get(changeClass.getClassId())==null?new CourseClass():courseClassMap.get(changeClass.getClassId());
                ChangeClassDetailVo changeClassDetailVo=new ChangeClassDetailVo();
                Student student=studentMap.get(changeClass.getStudentId());
                changeClassDetailVo.setStudentId(changeClass.getStudentId());
                if (student!=null){
                    changeClassDetailVo.setStudentName(student.getName());
                    changeClassDetailVo.setStudentPhoneNo(student.getPhoneNo());
                }
                changeClassDetailVo.setOperatorTime(changeClass.getCreateTime());
                changeClassDetailVo.setOperatorName(changeClass.getOperator());
                changeClassDetailVo.setOldClassBusTeamId(oldCourseClass.getBusTeamId());
                changeClassDetailVo.setOldClassId(oldCourseClass.getClassId());
                changeClassDetailVo.setOldClassName(oldCourseClass.getClassName());
                changeClassDetailVo.setOldClassBusTeamName(oldCourseClass.getBusTeamName());
                changeClassDetailVo.setOldTeacherId(oldCourseClass.getTeacherId());
                changeClassDetailVo.setOldTeacherName(oldCourseClass.getTeacherName());
                changeClassDetailVo.setStartAmount(changeClass.getStartAmount());
                changeClassDetailVo.setEndAmount(changeClass.getEndAmount());
                changeClassDetailVo.setClassId(courseClass.getClassId());
                changeClassDetailVo.setClassName(courseClass.getClassName());
                changeClassDetailVo.setBusTeamId(courseClass.getBusTeamId());
                changeClassDetailVo.setBusTeamName(courseClass.getBusTeamName());
                changeClassDetailVo.setTeacherId(courseClass.getTeacherId());
                changeClassDetailVo.setTeacherName(courseClass.getTeacherName());
                changeClassDetailVo.setReason(changeClass.getReason());
                changeClassDetailVo.setBalanceAmount(changeClass.getBalanceAmount());
                changeClassDetailVoList.add(changeClassDetailVo);
            }
            changeClassDetailVoPage.setRecords(changeClassDetailVoList);
        }
        return  changeClassDetailVoPage;

    }

    @Override
    public List<ChangeClass> findChangeClassByRegIds(List<Long> regIds,String ...columns) {
        if (Utils.checkListEmpty(regIds)){
            return Collections.EMPTY_LIST;
        }
        Example<ChangeClass> example=ChangeClass.createExample();
        if (columns!=null){
            example.selectProperties(columns);
        }
        example.createCriteria().andIn("regId",regIds);
        return this.findListByExample(example);
    }
}